package java实验六;

import java.util.Scanner;

public class 程序一 {
    public static void main(String[] args) {
        int[][] m = getArray();
        System.out.println("Sum of all elements is " + sum(m));
    }

    // Create a Scanner to read input
    public static Scanner defScanner() {
        return new Scanner(System.in);
    }

    // Get an array from user
    public static int[][] getArray() {
        Scanner input = defScanner();

        // Enter array values
        System.out.print("Enter rows and columns: ");
        int rows = input.nextInt();
        int columns = input.nextInt();

        int[][] m = new int[rows][columns];

        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[i].length; j++) {
                m[i][j] = input.nextInt();
            }
        }

        return m;
    }

    // Calculate the sum of all elements in the array
    public static int sum(int[][] m) {
        int total = 0;

        for (int row = 0; row < m.length; row++) {
            for (int column = 0; column < m[row].length; column++) {
                total += m[row][column];
            }
        }

        return total;
    }
}
